package com.cbank.controller.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cbank.constants.Result;
import com.cbank.constants.ResultCode;
import com.cbank.domain.Company;
import com.cbank.domain.User;
import com.cbank.domain.UserCompany;
import com.cbank.service.CompanyService;
import com.cbank.service.UserCompanyService;
import com.cbank.service.UserService;
import com.cbank.utils.UniqueKeyGenerator;

@Controller
@RequestMapping("/web/company")
public class WebCompanyController {
	@Autowired
	private CompanyService companyService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserCompanyService userCompanyService;
	Logger logger = Logger.getLogger(WebCompanyController.class);
	/**
	 * 最大的条数
	 */
	public static final int PAGER_SIZE = 1000;

	/**
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	public Result<List<Company>> userDetail(@CookieValue(value = "uid", defaultValue = "") long uid,
			@CookieValue(value = "token", defaultValue = "") String token, @RequestParam(value = "cityid") long cityid,
			@RequestParam(value = "industryid") long industryid, HttpServletResponse response, Model model) {
		int index = 0;
		int pagersize = PAGER_SIZE;
		if (pagersize > PAGER_SIZE || pagersize <= 0) {// 防止数据量太大
			pagersize = PAGER_SIZE;
		}
		User user = userService.getByViewIdAndToken(uid, token);
		// 用户不存在
		if (user == null) {
			return new Result<List<Company>>(ResultCode.ERROR_NOUSER.getCode(), ResultCode.ERROR_NOUSER.getMsg(),
					new ArrayList<Company>(0));
		} else {
			long uidL = user.getId();
			int count = userCompanyService.getTotalCountByUserId(uidL);// 今天已经下载数量
			Long canDownNum = user.getCanDownloadEachDay() - count;// 可下载数量
			if (canDownNum <= 0) {// 没有下载额度
				return new Result<List<Company>>(ResultCode.ERROR_BALANCE.getCode(), ResultCode.ERROR_BALANCE.getMsg(),
						new ArrayList<Company>(0));
			} else {
				pagersize = canDownNum.intValue();// 取出剩余的额度
				List<Company> companys = companyService.getList(uidL, index, pagersize, cityid, industryid);
				for (Company company : companys) {// 存入历史记录
					String phone = company.getPhone().trim();
					UserCompany userCompany = new UserCompany();
					userCompany.setPhoneNum(phone);
					userCompany.setCompanyTableId(company.getId());
					userCompany.setUserId(uidL);
					userCompany.setStatus(1);
					userCompany.setViewId(UniqueKeyGenerator.generateViewId());
					userCompanyService.save(userCompany);
				}
				return new Result<List<Company>>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), companys);
			}
		}
	}
}
